//
//  DnRollMenuTableView.m
//  DnRollTabbarMenu
//
//  Created by Ngoc on 27/11/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DnRollMenuTableView.h"

@interface DnRollMenuTableView(private)
-(void) createTable;
@end

@implementation DnRollMenuTableView
@synthesize menuArray, reusableCells, distanceBetweenItems, dataSource =_dataSource;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //[self createTable];
    }
    return self;
}
-(id) initWithFrame:(CGRect)frame distanceBetweenItems:(NSInteger)distance
{
    [self initWithFrame:frame];
    if (self) {
        self.distanceBetweenItems = distance;
        [self createTable];
    }
    return self;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{

    DnRollHorizontalMenuTableCell *cell = [menuDataSource objectAtIndex:0];
    return cell ;
}
-(void) createTable
{
    internalTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) ];
    internalTableView.delegate = self;
    internalTableView.dataSource = self;
    internalTableView.showsVerticalScrollIndicator = NO;
    internalTableView.showsHorizontalScrollIndicator = NO;
    internalTableView.scrollEnabled = NO;
    internalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    internalTableView.backgroundColor = [UIColor clearColor]; 
    internalTableView.tableFooterView =[[UIView alloc]initWithFrame:CGRectZero];
    internalTableView.rowHeight = kMenuItemHeight;
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, internalTableView.frame.size.width, internalTableView.frame.size.height);
    [self addSubview:internalTableView];
    
}
-(void) goToMenu:(NSInteger)nextMenuIndex fromMenu:(NSInteger)currentMenuIndex
{
    if (internalTableView!=nil) {
        [internalTableView beginUpdates];        
        if (nextMenuIndex>[self.dataSource numberMenuOfTable:self]-1) return;
        menuDataSource = [NSMutableArray array];
        DnRollHorizontalMenuTableCell *menu = (DnRollHorizontalMenuTableCell*) [self.dataSource menuSection:self atIndex:nextMenuIndex];
        if (nextMenuIndex!=currentMenuIndex) {
            if (nextMenuIndex<currentMenuIndex) {
                [internalTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
                [menuDataSource addObject:menu];
                [internalTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop]; 
            }
            else
            {
                [internalTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
                [menuDataSource addObject:menu];
                [internalTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom]; 
            }
            
        }
        
        [internalTableView endUpdates];
    }
}
//====================================================================================
-(void) layoutSubviews
{
    if ([menuDataSource count]<=0) {
        menuDataSource = [[NSMutableArray alloc]init ];
        [menuDataSource addObject:[self.dataSource menuSection:self atIndex:0]];
    }
}
- (void)dealloc
{
    self.menuArray = nil;
    self.reusableCells = nil;
    [super dealloc];
}

@end
